package net.sf.nlp.ltag.data;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import net.sf.nlp.ltag.util.LTAG;

public class SuperLTAG 
{
	private static final String SEPERATOR_REG = ", *";
	private static List<String> posPhraseSupports;
	
	private LTAGTree superTree;
	private List<ExtractedTree> etrees = new ArrayList<ExtractedTree>();
	
	private Boolean ignore = null;
	
	static
	{
		String posPhraseSupportStr = LTAG.postagSetProperties.getProperty("POS_PHRASE_SUPPORT");
		posPhraseSupports = Arrays.asList(posPhraseSupportStr.split(SEPERATOR_REG));
	}
	
	public LTAGTree getSuperTree() {
		return superTree;
	}
	public void setSuperTree(LTAGTree superTree) {
		this.superTree = superTree;
	}
	public List<ExtractedTree> getEtrees() {
		return etrees;
	}
	public void setEtrees(List<ExtractedTree> etrees) {
		this.etrees = etrees;
	}
	
	public boolean isIgnore() 
	{
		if (ignore == null)
		{
			ignore = false;
			validatePos(superTree.getRoot());
			validateLevel(superTree.getRoot());
		}
		
		return ignore;
	}
	
	public void validatePos(LTAGNode node)
	{
		if (!posPhraseSupports.contains(node.getNodeText()))
		{
			ignore = true;
			return;
		}
		
		if (node.getChildren() == null || node.getChildren().size() == 0)
			return;
		
		for (LTAGNode child : node.getChildren())
			validatePos(child);
	}
	
	private void validateLevel(LTAGNode node)
	{
		if (node.getLevel() > 3)
			ignore = true;
	}
	
	public List<LTAGMergedNode> getMergedNodes()
	{
		List<LTAGMergedNode> mergedNodes = new ArrayList<LTAGMergedNode>();
		
		getMergedNodes(superTree.getRoot(), mergedNodes);
		return mergedNodes;
	}
	
	private void getMergedNodes(LTAGNode node, List<LTAGMergedNode> mergedNodes)
	{
		if (node instanceof LTAGMergedNode)
			mergedNodes.add((LTAGMergedNode)node);
			
		if (node.getChildren() != null && node.getChildren().size() > 0)
		{
			for (LTAGNode child : node.getChildren())
				getMergedNodes(child, mergedNodes);
		}
	}
	
	public String toString()
	{
		return superTree.toSuperLTAG();
	}
	
	
}
